package com.example.neusoft.cook.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.neusoft.cook.R;
import com.example.neusoft.cook.ViewHolder.ShopitemViewHolder;
import com.example.neusoft.cook.activity.FoodListActivity;
import com.example.neusoft.cook.base.BaseAcyivity;
import com.example.neusoft.cook.base.BaseFragment;
import com.example.neusoft.cook.bean.SampleShop;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neusoft on 2017/5/3.
 */

public class ShopListAdapter extends RecyclerView.Adapter {
    private List<SampleShop> shopList = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;

    public ShopListAdapter(Context context,List<SampleShop> shopList){
        this.context = context;
        layoutInflater = layoutInflater.from(context);
        this.shopList.addAll(shopList);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//创建ViewHolder对象
        View itemView = layoutInflater.inflate(R.layout.item_shop_list,parent,false);
        ShopitemViewHolder holder = new ShopitemViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {//绑定数据
        ShopitemViewHolder shopitemViewHolder = (ShopitemViewHolder) holder;
        final SampleShop shop = shopList.get(position);
        final String shopName = shop.getShopname();
        String pic = shop.getPic();
        int level = shop.getLevel();
        String address = shop.getAddress();
        final int shopId = shop.getShop_id();
        shopitemViewHolder.shopNameView.setText(shopName);
        shopitemViewHolder.shopLevelView.setText(String.valueOf(level));
        shopitemViewHolder.shopAddressView.setText(address);
        if (!pic.equals("")){
            Picasso.with(context)
                    .load(pic)
                    .placeholder(R.drawable.ic_cook_holder)
                    .error(R.drawable.ic_cook_holder_error)
                    .into(shopitemViewHolder.imageView);
        }

        shopitemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动新的Activity--店铺详情（菜品列表）
                Toast.makeText(context,"店铺详情"+position,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, FoodListActivity.class);
                intent.putExtra("shop_id",shopId);
                intent.putExtra("shop_name",shopName);
                BaseAcyivity.phonenum = shop.getPhonenum();
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {//显示数据数量，看到几条数据
        return shopList.size();
    }
}
